local wezterm = require('wezterm')
local platform = require('utils.platform')()
local backdrops = require('utils.backdrops')
local act = wezterm.action

--WORKSPACES
-- wezterm.on('update-right-status', function(window, pane)
--    window:set_right_status(window:active_workspace())
-- end)
--

local mod = {}

if platform.is_mac then
   mod.SUPER = 'SUPER'
   mod.SUPER_REV = 'SUPER|CTRL'
elseif platform.is_win then
   mod.SUPER = 'ALT' -- to not conflict with Windows key shortcuts
   mod.SUPER_REV = 'ALT|CTRL'
end

local keys = {
   -- --WORKSPACES
   --
   -- {
   --    key = 'W',
   --    mods = 'CTRL|SHIFT',
   --    action = act.PromptInputLine({
   --       description = wezterm.format({
   --          { Attribute = { Intensity = 'Bold' } },
   --          { Foreground = { AnsiColor = 'Fuchsia' } },
   --          { Text = 'Enter name for new workspace' },
   --       }),
   --       action = wezterm.action_callback(function(window, pane, line)
   --          -- line will be `nil` if they hit escape without entering anything
   --          -- An empty string if they just hit enter
   --          -- Or the actual line of text they wrote
   --          if line then
   --             window:perform_action(
   --                act.SwitchToWorkspace({
   --                   name = line,
   --                }),
   --                pane
   --             )
   --          end
   --       end),
   --    }),
   -- },
   -- -- Switch to the default workspace
   -- {
   --    key = 'y',
   --    mods = 'CTRL|SHIFT',
   --    action = act.SwitchToWorkspace({
   --       name = 'default',
   --    }),
   -- },
   -- -- Switch to a monitoring workspace, which will have `top` launched into it
   -- {
   --    key = 'u',
   --    mods = 'CTRL|SHIFT',
   --    action = act.SwitchToWorkspace({
   --       name = 'monitoring',
   --       spawn = {
   --          args = { 'top' },
   --       },
   --    }),
   -- },
   -- -- Create a new workspace with a random name and switch to it
   -- { key = 'i', mods = 'CTRL|SHIFT', action = act.SwitchToWorkspace },
   -- -- Show the launcher in fuzzy selection mode and have it list all workspaces
   -- -- and allow activating one.
   -- {
   --    key = '9',
   --    mods = 'ALT',
   --    action = act.ShowLauncherArgs({
   --       flags = 'FUZZY|WORKSPACES',
   --    }),
   -- },
   --
   -- --

   -- misc/useful --
   { key = 'F1', mods = 'NONE', action = 'ActivateCopyMode' },
   { key = 'F2', mods = 'NONE', action = act.ActivateCommandPalette },
   { key = 's', mods = mod.SUPER_REV, action = act.ShowLauncher },
   { key = 'F4', mods = 'NONE', action = act.ShowLauncherArgs({ flags = 'FUZZY|TABS' }) },
   {
      key = 'F5',
      mods = 'NONE',
      action = act.ShowLauncherArgs({ flags = 'FUZZY|WORKSPACES' }),
   },
   { key = 'F12', mods = 'NONE', action = act.ShowDebugOverlay },
   { key = 'f', mods = mod.SUPER, action = act.Search({ CaseInSensitiveString = '' }) },
   {
      key = 'u',
      mods = mod.SUPER,
      action = wezterm.action.QuickSelectArgs({
         label = 'open url',
         patterns = {
            'https?://\\S+',
         },
         action = wezterm.action_callback(function(window, pane)
            local url = window:get_selection_text_for_pane(pane)
            wezterm.log_info('opening: ' .. url)
            wezterm.open_with(url)
         end),
      }),
   },

   -- copy/paste --
   { key = 'c', mods = 'CTRL|SHIFT', action = act.CopyTo('Clipboard') },
   { key = 'v', mods = 'CTRL|SHIFT', action = act.PasteFrom('Clipboard') },

   -- tabs --
   -- tabs: spawn+close
   -- { key = 't', mods = mod.SUPER, action = act.SpawnTab('DefaultDomain') },
   { key = 't', mods = mod.SUPER_REV, action = act.SpawnTab('DefaultDomain') },
   -- { key = 't', mods = mod.SUPER_REV, action = act.SpawnTab({ DomainName = 'WSL:Ubuntu' }) },
   { key = 'w', mods = mod.SUPER_REV, action = act.CloseCurrentTab({ confirm = false }) },

   -- tabs: navigation
   -- navigation toggle
   { key = '[', mods = mod.SUPER, action = act.ActivateTabRelative(-1) },
   { key = ']', mods = mod.SUPER, action = act.ActivateTabRelative(1) },
   { key = '[', mods = mod.SUPER_REV, action = act.MoveTabRelative(-1) },
   { key = ']', mods = mod.SUPER_REV, action = act.MoveTabRelative(1) },

   -- window --
   -- spawn windows
   { key = 'n', mods = mod.SUPER, action = act.SpawnWindow },

   -- background controls --
   {
      key = [[/]],
      mods = mod.SUPER,
      action = wezterm.action_callback(function(window, _pane)
         backdrops:random(window)
      end),
   },
   {
      key = [[,]],
      mods = mod.SUPER,
      action = wezterm.action_callback(function(window, _pane)
         backdrops:cycle_back(window)
      end),
   },
   {
      key = [[.]],
      mods = mod.SUPER,
      action = wezterm.action_callback(function(window, _pane)
         backdrops:cycle_forward(window)
      end),
   },
   {
      key = [[/]],
      mods = mod.SUPER_REV,
      action = act.InputSelector({
         title = 'Select Background',
         choices = backdrops:choices(),
         fuzzy = true,
         fuzzy_description = 'Select Background: ',
         action = wezterm.action_callback(function(window, _pane, idx)
            ---@diagnostic disable-next-line: param-type-mismatch
            backdrops:set_img(window, tonumber(idx))
         end),
      }),
   },

   -- --TODO 打开当前文件夹的 lazygit
   -- {
   --    key = 'g',
   --    mods = 'CTRL',
   --    action = wezterm.action.ExecCommand('lazygit', { args = { '--', '.' } }),
   -- },
   -- panes --
   -- panes: split panes
   {
      key = [[\]],
      mods = mod.SUPER,
      action = act.SplitVertical({ domain = 'CurrentPaneDomain' }),
   },
   {
      key = [[\]],
      mods = mod.SUPER_REV,
      action = act.SplitHorizontal({ domain = 'CurrentPaneDomain' }),
   },

   -- panes: zoom+close pane
   -- 折叠不关闭
   { key = 'z', mods = mod.SUPER_REV, action = act.TogglePaneZoomState },
   { key = 'w', mods = mod.SUPER, action = act.CloseCurrentPane({ confirm = false }) },

   -- panes: navigation
   { key = 'k', mods = mod.SUPER_REV, action = act.ActivatePaneDirection('Up') },
   { key = 'j', mods = mod.SUPER_REV, action = act.ActivatePaneDirection('Down') },
   { key = 'h', mods = mod.SUPER_REV, action = act.ActivatePaneDirection('Left') },
   { key = 'l', mods = mod.SUPER_REV, action = act.ActivatePaneDirection('Right') },

   -- panes: resize
   { key = 'o', mods = mod.SUPER_REV, action = act.AdjustPaneSize({ 'Up', 1 }) },
   { key = 'i', mods = mod.SUPER_REV, action = act.AdjustPaneSize({ 'Down', 1 }) },
   { key = 'p', mods = mod.SUPER_REV, action = act.AdjustPaneSize({ 'Left', 1 }) },
   { key = 'u', mods = mod.SUPER_REV, action = act.AdjustPaneSize({ 'Right', 1 }) },

   --使用映射方式每个pane标号，按下对应的数字就跳到对应的pane
   {
      key = 'f',
      mods = mod.SUPER_REV,
      action = act.PaneSelect({ alphabet = '1234567890', mode = 'SwapWithActiveKeepFocus' }),
   },

   -- key-tables --
   -- resizes fonts
   -- 字体变大
   {
      key = 'f',
      mods = 'LEADER',
      action = act.ActivateKeyTable({
         name = 'resize_font',
         one_shot = false,
         timemout_miliseconds = 1000,
      }),
   },
   -- resize panes
   -- 面板变大
   {
      key = 'p',
      mods = 'LEADER',
      action = act.ActivateKeyTable({
         name = 'resize_pane',
         one_shot = false,
         timemout_miliseconds = 1000,
      }),
   },
}

local key_tables = {
   resize_font = {
      { key = 'k', action = act.IncreaseFontSize },
      { key = 'j', action = act.DecreaseFontSize },
      { key = 'r', action = act.ResetFontSize },
      { key = 'Escape', action = 'PopKeyTable' },
      { key = 'q', action = 'PopKeyTable' },
   },
   resize_pane = {
      { key = 'k', action = act.AdjustPaneSize({ 'Up', 1 }) },
      { key = 'j', action = act.AdjustPaneSize({ 'Down', 1 }) },
      { key = 'h', action = act.AdjustPaneSize({ 'Left', 1 }) },
      { key = 'l', action = act.AdjustPaneSize({ 'Right', 1 }) },
      { key = 'Escape', action = 'PopKeyTable' },
      { key = 'q', action = 'PopKeyTable' },
   },
}

local mouse_bindings = {
   -- Ctrl-click will open the link under the mouse cursor
   {
      event = { Up = { streak = 1, button = 'Left' } },
      mods = 'CTRL',
      action = act.OpenLinkAtMouseCursor,
   },
}

return {
   disable_default_key_bindings = true,
   -- leader = { key = 'Space', mods = mod.SUPER_REV },
   -- 这个的意思是space在super-rev模式下是leader键的意思
   leader = { key = 'Space', mods = mod.SUPER_REV },
   keys = keys,
   key_tables = key_tables,
   mouse_bindings = mouse_bindings,
}
